package cc.leap.service;

import cc.leap.constant.UserConstants;
import cc.leap.mapper.SysPostMapper;
import cc.leap.model.PageQuery;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysPost;
import cc.leap.web.vo.server.Sys;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

import static cc.leap.model.domain.table.SysPostTableDef.SYS_POST;
import static cc.leap.model.domain.table.SysUserPostTableDef.SYS_USER_POST;
import static cc.leap.model.domain.table.SysUserTableDef.SYS_USER;

/**
 * SysPostService
 *
 * @author leap
 * @date 2024/8/19
 */
@Component
public class SysPostService extends ServiceImpl<SysPostMapper, SysPost> {

    @Inject
    SysPostMapper postMapper;

    /**
     * 根据postBo构建QueryWrapper查询条件
     *
     * @param postBo
     * @return 查询条件
     */
    private QueryWrapper buildQueryWrapper(SysPost postBo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_POST.POST_ID, SYS_POST.POST_NAME, SYS_POST.POST_CODE, SYS_POST.POST_SORT, SYS_POST.STATUS, SYS_POST.CREATE_TIME)
                .and(SYS_POST.POST_CODE.like(postBo.getPostCode()))
                .and(SYS_POST.STATUS.eq(postBo.getStatus()))
                .and(SYS_POST.POST_NAME.like(postBo.getPostName()));
        return queryWrapper;
    }

    public List<SysPost> selectPostAll() {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.select(SYS_POST.POST_ID, SYS_POST.POST_CODE, SYS_POST.POST_NAME, SYS_POST.POST_SORT)
                .from(SYS_POST);
        return this.list(queryWrapper);
    }

    public List<Long> selectPostListByUserId(Long userId) {
        QueryWrapper queryWrapper = QueryWrapper.create().select(SYS_POST.POST_ID)
                .from(SYS_POST).as("p")
                .leftJoin(SYS_USER_POST).as("up").on(SYS_USER_POST.POST_ID.eq(SYS_POST.POST_ID))
                .leftJoin(SYS_USER).as("u").on(SYS_USER.USER_ID.eq(SYS_USER_POST.USER_ID))
                .where(SYS_USER.USER_ID.eq(userId));
        return this.listAs(queryWrapper, Long.class);
    }

    public TableDataInfo<SysPost> selectPostPage(SysPost post) {
        QueryWrapper queryWrapper = buildQueryWrapper(post);
        Page<SysPost> page = this.pageAs(PageQuery.build(), queryWrapper, SysPost.class);
        return TableDataInfo.build(page);
    }

    public SysPost selectPostById(Long postId) {
        return postMapper.selectPostById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    public int deletePostByIds(Long[] postIds) {
        return postMapper.deletePostByIds(postIds);
    }

    /**
     * 修改岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    public int updatePost(SysPost post) {
        return postMapper.updatePost(post);
    }

    /**
     * 新增岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    public int insertPost(SysPost post) {
        return postMapper.insertPost(post);
    }

    /**
     * 校验岗位名称
     *
     * @param post 岗位
     * @return 结果
     */
    public boolean checkPostNameUnique(SysPost post) {
        Long postId = ObjectUtil.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostNameUnique(post.getPostName());
        if (ObjectUtil.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码
     *
     * @param post 岗位
     * @return 结果
     */
    public boolean checkPostCodeUnique(SysPost post) {
        Long postId = ObjectUtil.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostCodeUnique(post.getPostCode());
        if (ObjectUtil.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
